package main.java.easy;

/**
 * @Author: 郑国荣
 * @Date: 2023-07-23-12:00
 * @Description:
 */
public class Mysqrt {
     public static int sqrt ;
    public static int mySqrt(int x){
        sqrt =x;
        if (x==0||x==1) return x;
        return (int) sqrt(x);
    }

    /**
     *  f(x)=x^2-a 的斜率接近2x
     *  f(x)=0 求x
     *  f(x)/2x = x/2-a/2x -> 0
     *  x-f(x)/2x -> x
     *  x-(x^2-a)/2x=x-x/2+a/2x=x/2+a/2x=
     *  (x+a/x)/2
     */
    private static double sqrt(double x) {
        double res = (x+sqrt/x)/2;
        if (res==x) return res;
        return sqrt(res);
    }

    public static void main(String[] args) {
        System.out.println((46340 * 46340));
        System.out.println(mySqrt(2147395600));
    }
}
